package com.ygqh.baby.service.impl;

import java.util.Date;
import java.util.List;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.po.YgWholesaler;
import com.ygqh.baby.po.YgWholesalerBillRecord;
import com.ygqh.baby.mapper.YgWholesalerBillRecordMapper;
import com.ygqh.baby.po.YgWholesalerPrebillRecord;
import com.ygqh.baby.service.YgWholesalerBillRecordService;
import com.ygqh.baby.service.YgWholesalerPrebillRecordService;
import com.ygqh.baby.service.YgWholesalerService;
import com.ygqh.baby.utils.DateConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import java.util.Arrays;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgWholesalerBillRecordExample;

/**
 * @author Ss
 */
@Service
public class YgWholesalerBillRecordServiceImpl implements YgWholesalerBillRecordService {

	@Autowired
	private YgWholesalerBillRecordMapper ygWholesalerBillRecordMapper;
	@Autowired
	private YgWholesalerService ygWholesalerService;
	@Autowired
	private YgWholesalerPrebillRecordService ygWholesalerPrebillRecordService;

	@Override
	public List<YgWholesalerBillRecord> find() {
		YgWholesalerBillRecordExample example = new YgWholesalerBillRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerBillRecordMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgWholesalerBillRecordExample example = new YgWholesalerBillRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerBillRecordMapper.countByExample(example);
	}

	@Override
	public YgWholesalerBillRecord findById(Long id) {
		return ygWholesalerBillRecordMapper.selectByPrimaryKey(id);
	}


	@Override
	public int save(YgWholesalerBillRecord ygWholesalerBillRecord) {
		return ygWholesalerBillRecordMapper.insertSelective(ygWholesalerBillRecord);
	}

	@Override
	public int update(YgWholesalerBillRecord ygWholesalerBillRecord) {
		return ygWholesalerBillRecordMapper.updateByPrimaryKeySelective(ygWholesalerBillRecord);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgWholesalerBillRecordExample example = new YgWholesalerBillRecordExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgWholesalerBillRecord record = new YgWholesalerBillRecord();
		record.setStatus(DataStatus.Delete);
		return ygWholesalerBillRecordMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgWholesalerBillRecord ygWholesalerBillRecord) {
		int r;
		if (ygWholesalerBillRecord.getId() != null) {
			r = this.update(ygWholesalerBillRecord);
		} else {
			r = this.save(ygWholesalerBillRecord);
		}
		return r;
	}

	@Override
	public ResultSet<YgWholesalerBillRecord> search(QueryInfo queryInfo, String wholesalerName, Date startDate, Date endDate, String billCode, BillStatus billStatus) {
		if (startDate != null) {
			startDate = DateConvertUtils.getDateStart(startDate);
		}
		if (endDate != null) {
			endDate = DateConvertUtils.addDay(DateConvertUtils.getDateStart(endDate), 1);
		}
		int count = ygWholesalerBillRecordMapper.countSuper(wholesalerName, startDate, endDate, billCode, billStatus);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgWholesalerBillRecord> list = ygWholesalerBillRecordMapper.selectSuper(queryInfo, wholesalerName, startDate, endDate, billCode, billStatus);
		return new ResultSet<>(count, list);
	}

	@Override
	public void updateBillStatus(YgWholesalerBillRecord billRecord) {
		this.validateParamForBill(billRecord);
		this.update(billRecord);

		if (BillStatus.Billed.equals(billRecord.getBillStatus())) {
			// 结算后，更新批发商已结金额
			YgWholesaler wholesaler = new YgWholesaler();
			wholesaler.setId(billRecord.getWholesalerId());
			wholesaler.setAmountDuePrice(billRecord.getAmountDuePrice());
			wholesaler.setAmountPrice(billRecord.getAmountPrice());
			wholesaler.setUpdateTime(billRecord.getUpdateTime());
			wholesaler.setUpdateBy(billRecord.getUpdateBy());
			ygWholesalerService.updateAmountPrice(wholesaler);
		} else if (BillStatus.Dismissed.equals(billRecord.getBillStatus())) {
			// 驳回后，清空关联预结单的结算单号
			YgWholesalerPrebillRecord record = new YgWholesalerPrebillRecord();
			record.setBillCode(billRecord.getBillCode());
			record.setCommitStatus(CommitStatus.WaitingCommit);
			record.setUpdateBy(billRecord.getUpdateBy());
			record.setUpdateTime(billRecord.getUpdateTime());
			ygWholesalerPrebillRecordService.updateByDismissedRecord(record);
		}
	}

	private void validateParamForBill(YgWholesalerBillRecord billRecord) {
		Assert.notNull(billRecord.getId(), "未指定结算单");
		Assert.notNull(billRecord.getBillStatus(), "结算单状态不能为空");
		YgWholesalerBillRecord billRecordFromDB = this.findById(billRecord.getId());
		Assert.notNull(billRecordFromDB, "指定结算单不存在");
		Assert.isTrue(BillStatus.WaitingBill.equals(billRecordFromDB.getBillStatus()), "指定结算单状态异常");
		billRecord.setBillCode(billRecordFromDB.getBillCode());
		billRecord.setWholesalerId(billRecordFromDB.getWholesalerId());
		billRecord.setAmountDuePrice(billRecordFromDB.getAmountDuePrice());
		if (BillStatus.Billed.equals(billRecord.getBillStatus())) {
			Assert.notNull(billRecord.getAmountPrice(), "结算金额不能为空");
		} else if (BillStatus.Dismissed.equals(billRecord.getBillStatus())) {
			Assert.isTrue(StringUtils.isNotBlank(billRecord.getRemark()), "驳回备注不能为空");
		} else {
			throw new IllegalArgumentException("暂不支持结算单更新为 " + billRecord.getBillStatusDesc());
		}
	}

}

